package com.whmskj.xjlhsc.mobie.personnelFile.controller;

import com.whmskj.xjlhbc.core.AjaxResult;
import com.whmskj.xjlhbc.core.Page;
import com.whmskj.xjlhbc.domain.Yg01employeefiles;
import com.whmskj.xjlhbc.service.Yg01employeefilesService;
import com.whmskj.xjlhsc.controller.vo.Result;
import com.whmskj.xjlhsc.mobie.personnelFile.service.PersonnelFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import jdk.jfr.StackTrace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Api(tags = "移动端-人员档案")
@RestController
@RequestMapping("/mob/personnel")
public class PersonnelFileController {
    @Resource
    private PersonnelFileService service;

    @Autowired
    private Yg01employeefilesService yg01employeefilesService;

    @ApiOperation(value = "人员档案列表（分页）", notes = "支持关键词、班组、部门、工种、性别、员工类型、是否无工序工人、班型(运转班/长白班)；分页参数 pageNum/pageSize 走 URL 查询参数。\nBody 示例：{keyword:'张三', class55:'一班', sector:'梳棉', typeOfWork:'工人', gender:'男', employeeType:'正式', shiftTypes:['运转班','长白班'], noProcessWorker:true}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码(默认1)", required = false, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数(默认10)", required = false, paramType = "query"),
            @ApiImplicitParam(name = "body", value = "查询条件JSON", required = false, paramType = "body")
    })
    @PostMapping("/files")
    public AjaxResult list(@RequestBody(required = false) Map<String, Object> body) {
        String keyword = body == null ? null : (String) body.get("keyword");
        String class55 = body == null ? null : (String) body.get("class55");
        String process = body == null ? null : (String) body.get("process");
        String typeOfWork = body == null ? null : (String) body.get("typeOfWork");
        String gender = body == null ? null : (String) body.get("gender");
        String employeeType = body == null ? null : (String) body.get("employeeType");
        Boolean noProcessWorker = body == null ? null : (Boolean) body.get("noProcessWorker");
        // 新增：班型筛选（运转班/长白班），支持字符串或数组
        Object shiftTypesObj = body == null ? null : body.get("shiftTypes");
        java.util.List<String> shiftTypes = null;
        if (shiftTypesObj instanceof String) {
            shiftTypes = java.util.Collections.singletonList((String) shiftTypesObj);
        } else if (shiftTypesObj instanceof java.util.List) {
            shiftTypes = ((java.util.List<?>) shiftTypesObj).stream().map(String::valueOf).collect(Collectors.toList());
        }

        Page.startPage();
        List<Map<String, Object>> data = service.list(keyword, class55, process, typeOfWork, gender, employeeType, noProcessWorker, shiftTypes);
        Map<String, Object> summary = service.summary();
        Map<String, Object> payload = new java.util.HashMap<>();
        payload.put("results", data);
        payload.put("total", com.github.pagehelper.PageInfo.of(data).getTotal());
        payload.put("summary", summary);
        return AjaxResult.success(payload);
    }

    @GetMapping("getPersonProcess")
    @Operation(description = "获取员工工序")
    public Result getPersonProcess(@RequestParam String workShop) {
       List<String> list= service.getPersonProcess(workShop);
       return Result.success(list);
    }


    @GetMapping("/files/{id}")
    public AjaxResult detail(@PathVariable Long id) {
        return AjaxResult.success(service.detail(id));
    }

    @GetMapping("/filters")
    public AjaxResult filters() {
        return AjaxResult.success(service.filters());
    }

    @PostMapping("abjustEmpTypeOfWork")
    @Operation(description = "调整员工工种")
    public Result yg01employeefilesService(@RequestBody Yg01employeefiles yg01employeefiles) {
        yg01employeefilesService.yg01employeefilesService(yg01employeefiles);
        return Result.success();
    }


}
